package com.centaline.androidsalesblog.base;

import android.app.ProgressDialog;
import android.view.View;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.application.MyLocation;
import com.centaline.androidsalesblog.iml.DropCompleteListener;
import com.centaline.androidsalesblog.model.databaseobject.CityCenterDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.rx.map.RxMap;
import com.centaline.androidsalesblog.widgets.dropdown.DropDownView;
import com.orhanobut.logger.Logger;
import com.trello.rxlifecycle.FragmentEvent;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 地图找房基类
 * Created by guilin on 16/4/13.
 */
public abstract class BaseMainMapFragment extends BaseFragment implements BaiduMap.OnMapLoadedCallback,
        BaiduMap.OnMapStatusChangeListener {

    protected DropDownView dropDownView;
    protected MapView mapView;
    protected BaiduMap baiduMap;
    protected ProgressDialog loadingDialog;

    protected DropCompleteListener dropCompleteListener;
    protected LatLng cityCenter;//城市中心点
    protected boolean hasInitRequest;//是否初始化请求
    protected int mapStatusChangeTag;//地图状态改变标签
    protected float startZoom;//开始zoom级别
    protected float endZoom;//开始zoom级别
    protected int distance;//定位时距离

    protected LocationClient locationClient;

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        mapView.onDestroy();
        super.onDestroy();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_main_map;
    }

    @Override
    protected void findViews(View view) {
        dropDownView = (DropDownView) view.findViewById(R.id.dropDownView);
        mapView = (MapView) view.findViewById(R.id.mapView);
    }

    @Override
    protected void initView() {
        dropDownView.setDropCompleteListener(getDropCompleteListener());
        baiduMap = mapView.getMap();
        UiSettings uiSettings = baiduMap.getUiSettings();
        uiSettings.setCompassEnabled(true);
        uiSettings.setOverlookingGesturesEnabled(false);
        uiSettings.setRotateGesturesEnabled(false);
        baiduMap.setMyLocationEnabled(true);
        baiduMap.setOnMapStatusChangeListener(this);
        baiduMap.setOnMapLoadedCallback(this);

        locationClient = MyLocation.getLocationClient();
        RxMap.location(locationClient, false)
                .compose(this.<BDLocation>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<BDLocation>() {
                    @Override
                    public void call(BDLocation bdLocation) {
                        locationInfo(bdLocation);
                    }
                });
        initDrop();
        initOther();
    }

    @Override
    protected void loadData() {
        initRequest();
    }

    /**
     * 下拉菜单选择事件
     */
    protected abstract DropCompleteListener getDropCompleteListener();

    /**
     * 初始化下拉菜单
     */
    protected abstract void initDrop();

    /**
     * 初始化其他
     */
    protected abstract void initOther();

    @Override
    public void onMapLoaded() {
        mapView.showZoomControls(false);
        baiduMap.setMaxAndMinZoomLevel(18f, 9f);
        RxDataBase.cityCenterDo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<CityCenterDo>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<CityCenterDo>() {
                    @Override
                    public void call(CityCenterDo cityCenterDo) {
                        cityCenter = new LatLng(cityCenterDo.getLat(), cityCenterDo.getLng());
                        baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngZoom(cityCenter, 9f));
                    }
                });
    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {
        startZoom = mapStatus.zoom;
        Logger.d("onMapStatusChangeStart zoom : %f", startZoom);
    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        endZoom = mapStatus.zoom;
        Logger.d("onMapStatusChangeFinish zoom : %f", endZoom);
    }

    /**
     * 初始化请求
     */
    protected void initRequest() {
        if (!hasInitRequest) {
            hasInitRequest = true;
            Observable
                    .interval(0, 1, TimeUnit.SECONDS)
                    .take(3)
                    .observeOn(AndroidSchedulers.mainThread())
                    .compose(this.<Long>bindUntilEvent(FragmentEvent.DESTROY))
                    .subscribe(new Action1<Long>() {
                        @Override
                        public void call(Long aLong) {
                            if (aLong == 1) {
                                requestList(false);
                            }
                        }
                    });
        }
    }

    /**
     * 请求列表
     *
     * @param needLocation 是否需要登录信息
     */
    protected void requestList(boolean needLocation) {

    }

    /**
     * 重置标签
     *
     * @param position 位置
     */
    protected void resetTab(int position) {
        dropDownView.resetTab(position);
    }

    /**
     * 设置标签
     *
     * @param position 位置
     * @param tab      文本
     */
    protected void setTab(int position, String tab) {
        dropDownView.setTab(position, tab);
    }

    /**
     * 开始定位
     */
    protected void startLocation() {
        locationClient.start();
    }

    /**
     * 定位信息
     */
    protected void locationInfo(BDLocation bdLocation) {

    }
}
